﻿Imports System.Security.Cryptography
Imports TBCC.Utility

Public Class UserClass

    ''' <summary>
    ''' 创建用户
    ''' </summary>
    ''' <param name="uID"></param>
    ''' <param name="uName"></param>
    ''' <param name="uPwd"></param>
    ''' <returns>0表示创建成功,1表示用户已经存在，其它表示创建失败。</returns>
    ''' <remarks></remarks>
    Public Shared Function Create(ByVal uID As String, ByVal uName As String, ByVal uPwd As String, ByVal roleID As Integer) As Integer
        Dim strSql As String
        strSql = "Select Count(*) From tbccUser Where uid=?p0"
        Dim p1(0) As MySql.Data.MySqlClient.MySqlParameter
        p1(0) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(0).ParameterName = "p0" : p1(0).Value = uID

        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, p1)

        If CInt(obj) = 1 Then
            'Throw New Exception("用户已经存在。")
            Return 1
        End If

        Dim tic As New TranstrationItem

        strSql = "Insert Into tbccUser(uid,uName,uPassword,uEnable,uCreated) Values(?p0,?p1,?p2,?p3,?p4)"
        Dim p2(4) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p2.Length - 1
            p2(i) = New MySql.Data.MySqlClient.MySqlParameter
        Next
        p2(0).ParameterName = "p0" : p2(0).Value = uID
        p2(1).ParameterName = "p1" : p2(1).Value = uName
        p2(2).ParameterName = "p2" : p2(2).Value = UserClass.GetMD5(uPwd)
        p2(3).ParameterName = "p3" : p2(3).Value = 1
        p2(4).ParameterName = "p4" : p2(4).Value = Now.ToString("yyyy-MM-dd HH:mm:ss")
        tic.Add(strSql, p2)

        strSql = "Insert Into tbccUserRole(uID,roleID) Values(?p0,?p1)"
        Dim p3(1) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p3.Length - 1
            p3(i) = New MySql.Data.MySqlClient.MySqlParameter
        Next
        p3(0).ParameterName = "p0" : p3(0).Value = uID
        p3(1).ParameterName = "p1" : p3(1).Value = roleID

        tic.Add(strSql, p3)

        Dim result As Boolean
        result = SqlHelper.ExecuteTranstration(tic)

        If Not result Then
            Return -1
        End If

        Return 0
    End Function

    ''' <summary>
    ''' 修改用户信息
    ''' </summary>
    ''' <param name="uID"></param>
    ''' <param name="uName"></param>
    ''' <param name="uPwd"></param>
    ''' <returns>0表示创建成功,1表示不用户存在，其它表示创建失败。</returns>
    ''' <remarks></remarks>
    Public Shared Function Update(ByVal uID As String, ByVal uName As String, ByVal uPwd As String, ByVal roleID As Integer) As Integer
        Dim strSql As String
        strSql = "Select Count(*) From tbccUser Where uid=?p0"
        Dim p1(0) As MySql.Data.MySqlClient.MySqlParameter
        p1(0) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(0).ParameterName = "p0" : p1(0).Value = uID
        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, p1)

        If CInt(obj) <> 1 Then
            'Throw New Exception("用户已经存在。")
            Return 1
        End If

        Dim tic As New TranstrationItem


        If uPwd = "" Then
            strSql = "Update tbccUser Set uName=?p0,uEnable=?p1 where uid=?p2"
            Dim p2(2) As MySql.Data.MySqlClient.MySqlParameter
            For i As Integer = 0 To p2.Length - 1
                p2(i) = New MySql.Data.MySqlClient.MySqlParameter
            Next
            p2(0).ParameterName = "p0" : p2(0).Value = uName
            p2(1).ParameterName = "p1" : p2(1).Value = 1
            p2(2).ParameterName = "p2" : p2(2).Value = uID
            tic.Add(strSql, p2)
        Else
            strSql = "Update tbccUser Set uName=?p0,uPassword=?p1,uEnable=?p2 where uid=?p3"
            Dim p2(3) As MySql.Data.MySqlClient.MySqlParameter
            For i As Integer = 0 To p2.Length - 1
                p2(i) = New MySql.Data.MySqlClient.MySqlParameter
            Next

            p2(0).ParameterName = "p0" : p2(0).Value = uName
            p2(1).ParameterName = "p1" : p2(1).Value = UserClass.GetMD5(uPwd)
            p2(2).ParameterName = "p2" : p2(2).Value = 1
            p2(3).ParameterName = "p3" : p2(3).Value = uID

            tic.Add(strSql, p2)

        End If


        strSql = "Delete From tbccUserRole where uID=?p0"
        Dim p3(0) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p3.Length - 1
            p3(i) = New MySql.Data.MySqlClient.MySqlParameter
        Next
        p3(0).ParameterName = "p0" : p3(0).Value = uID
        tic.Add(strSql, p3)

        strSql = "Insert Into tbccUserRole(uID,roleID) Values(?p0,?p1)"
        Dim p4(1) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p4.Length - 1
            p4(i) = New MySql.Data.MySqlClient.MySqlParameter
        Next
        p4(0).ParameterName = "p0" : p4(0).Value = uID
        p4(1).ParameterName = "p1" : p4(1).Value = roleID
        tic.Add(strSql, p4)

        Dim result As Boolean
        result = SqlHelper.ExecuteTranstration(tic)

        If Not result Then
            Return -1
        End If

        Return 0
    End Function

    ''' <summary>
    ''' 删除用户
    ''' </summary>
    ''' <param name="uID"></param>
    ''' <returns>0表示删除成功，1表示用户不存在，其它表示删除失败</returns>
    ''' <remarks></remarks>
    Public Shared Function Delete(ByVal uID As String) As Integer
        Dim strSql As String
        strSql = "Select Count(*) From tbccUser Where uid = ?p0"
        Dim p1(0) As MySql.Data.MySqlClient.MySqlParameter
        p1(0) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(0).ParameterName = "p0" : p1(0).Value = uID

        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, p1)

        If CInt(obj) <> 1 Then
            'Throw New Exception("用户已经存在。")
            Return 1
        End If

        Dim tic As New TranstrationItem

        strSql = "Delete From tbccUser where uid=?p0"
        Dim p2(0) As MySql.Data.MySqlClient.MySqlParameter
        p2(0) = New MySql.Data.MySqlClient.MySqlParameter()
        p2(0).ParameterName = "p0" : p2(0).Value = uID
        tic.Add(strSql, p2)

        strSql = "Delete From tbccUserRole where uid=?p0"
        tic.Add(strSql, p2)

        Dim result As Integer
        'result = SqlHelper.ExecuteNonQuery(strSql, p2)
        If SqlHelper.ExecuteTranstration(tic) Then
            result = 1
        Else
            result = 0
        End If

        If result <> 1 Then
            Return -1
        End If

        Return 0
    End Function


    ''' <summary>
    ''' 计算Hash值
    ''' </summary>
    ''' <param name="input"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Friend Shared Function GetMD5(ByVal input As String) As String
        Dim d5 As MD5
        d5 = MD5CryptoServiceProvider.Create()
        Dim b() As Byte
        b = d5.ComputeHash(System.Text.Encoding.Unicode.GetBytes(input, 0, input.Length))
        Return Convert.ToBase64String(b)
    End Function

    Public Shared Function GetUserList() As DataTable
        Dim strSql As String
        'strSql = "Select * From tbccUser"
        strSql = "Select a.uID,a.uname,a.uenable,b.roleid,c.RoleName,c.RoleDisplayName from tbccUser a , tbccuserrole b,tbccRoles c where a.uID =b.uID and b.roleID=c.RoleID and a.uID<>'admin' and a.uID<>'thermoberg'"

        Dim ds As DataSet
        ds = SqlHelper.ExecuteDataset(strSql, Nothing)

        Return ds.Tables(0)
    End Function



End Class
